package com.presalept.manipulatedata;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.presalept.polodo.EventDO;

public class CreateEvent {
	
	public static int InsertEvent(EventDO eventdo) throws SQLException{
		int InsertResult = 0;
		Connection dbConn = null;
		PreparedStatement dbEventStmt = null;
		PreparedStatement dbEventUserStmt = null;
		PreparedStatement dbProjectUserStmt = null;
		ResultSet dbSelectRs = null;
		ResultSet dbInsertRs = null; 
		
		List<Integer> UserIdList = new ArrayList<Integer>();
		int insertEventResult = 0;
		int[] insertEventUserResult;
		
		String sqlGetProjectUser = "selet userid from relationofprojectanduser where projectid = ?";
		
		String sqlInsertEvent = "INSERT INTO event (eventname,projectidofevent,mainparticipant,eventdescription,voicerecordurl,createrid,createdate,updatestatus,feedbackid)"
				+ " VALUES(?,?,?,?,?,?,?,?,?); ";
		
		String sqlInsertEventUser = "INSERT INTO relationofeventanduser (eventid,userid,eurelationstatus,createdate)" 
				+ " VALUES(?,?,?,?); ";
		
		try{
			
			dbConn = DbManager.getConnection();		
			
			dbProjectUserStmt = dbConn.prepareStatement(sqlGetProjectUser);
			dbEventStmt = dbConn.prepareStatement(sqlInsertEvent);
			dbEventUserStmt = dbConn.prepareStatement(sqlInsertEventUser);
			
			//get userids by the projectis of the eventid;

			dbProjectUserStmt.setInt(1, eventdo.getProjectIdofEvent());
			
			dbSelectRs = dbProjectUserStmt.executeQuery();
			while(dbSelectRs.next()){
				UserIdList.add(dbSelectRs.getInt("userid"));				
			}
			
			//insert event
			dbConn.setAutoCommit(false);
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");  
	        Date createdate = null;  
	        try {  
	        	createdate = sdf.parse(eventdo.getCreateDate()); 
	            
	        } catch (ParseException e) {  
	            e.printStackTrace();  
	        }       
	        java.sql.Date sqlcreateDate = new java.sql.Date(createdate.getTime()); 
			dbEventStmt.setString(1, eventdo.getEventName());
			dbEventStmt.setInt(2, eventdo.getProjectIdofEvent());
			dbEventStmt.setString(3, eventdo.getMainparticipant());
			dbEventStmt.setString(4, eventdo.getEventDescription());
			dbEventStmt.setString(5, eventdo.getVoicerecordurl());
			dbEventStmt.setInt(6, eventdo.getCreaterid());
			dbEventStmt.setDate(7, sqlcreateDate);
			dbEventStmt.setInt(8, eventdo.getUpdatestatus());
			dbEventStmt.setInt(9, eventdo.getFeedbackid());
			insertEventResult = dbEventStmt.executeUpdate(sqlInsertEvent);
			
			//insert users of the event if useridlist > 0
			
			if(insertEventResult > 0 && UserIdList.size() > 0){
				
				dbInsertRs = dbEventStmt.getGeneratedKeys();
				
				int CurrentEventId = dbInsertRs.getInt(1);
				for(Iterator<Integer> i = UserIdList.iterator(); i.hasNext();){
					
					dbEventUserStmt.setInt(1, CurrentEventId);
					dbEventUserStmt.setInt(2, i.next());
					dbEventUserStmt.setInt(3, 1);
					dbEventUserStmt.setDate(4, sqlcreateDate);
					
					dbEventUserStmt.addBatch();		
				}
				
				// batch insert users of the event
				
				insertEventUserResult = dbEventUserStmt.executeBatch();
				boolean isInsertRight = false;
				for(int i = 0; i < insertEventUserResult.length; i++){
					
					//if some record has insert error, then rollback insertting event
					
					if(insertEventUserResult[i] == 0){
						dbConn.rollback();
						break;
					}
					isInsertRight = true;
				}
				if(isInsertRight){
					dbConn.commit();
					InsertResult = 1;
				}
			}
		}finally{
			if (dbInsertRs != null) dbInsertRs.close();
			if (dbSelectRs != null) dbSelectRs.close();
			if (dbConn != null) dbConn.close();			
		}
		
		return InsertResult;
	}
	
	
	

}
